﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class mobile_ajaxdata_MyShop : BaseDataPage
{
    protected override void Page_Load(object sender, EventArgs e)
    {
         string aciton = Request.Params["aciton"];
         switch (aciton)
         {
             case "getmyshopmoney":
                 getmyshopmoney();
                 break;
             case "getmyshopdzlist":
                 getmyshopdzlist();
                 break;
             case "getmyshopdzdtl":
                 getmyshopdzdtl();
                 break;
             case "upshopstate":
                 upshopstate();
                 break;
             case "getmypaylog":
                 getmypaylog();
                 break;
             case "getaccount":
                 getaccount();
                 break;
             case "GetVipSaleByShop":
                 GetVipSaleByShop();
                 break;
             case "saveshopexpense":
                 saveshopexpense();
                 break;
             case "getshopexpenslist":
                 getshopexpenslist();
                 break;    
         }
    }

    private void getshopexpenslist()
    {
        string shopid = Request.Params["shopid"];
        string bdate = Request.Params["bdate"];
        string edate = Request.Params["edate"];
        if (bdate == "" || bdate ==null)
        {
            bdate = DateTime.Now.AddDays(-1).ToString("yyyy-MM-dd");
            edate = DateTime.Now.AddDays(-1).ToString("yyyy-MM-dd");
        }
        string pageindex = Request.Params["pageindex"];
        string pagecount = Request.Params["pagecount"];
        int showcount = Convert.ToInt32(pageindex) * Convert.ToInt32(pagecount);
        string sql = @"select top " + pagecount + @" *
                        from 
                        (
                            select row_number() over(order by PayDate desc) as rownumber,
                            convert(varchar(20),PayDate,23)as PayDate,PayMoney,Remark from Bas_ShopExpens where shopcode='" + shopid+ @"'
                            and  paydate between '"+bdate+"' and '"+edate+@"'
                        ) A
                        where rownumber > " + showcount.ToString();

        //Response.Write(sql);
        //Response.End();
        //return;
        GetDataToJsonBySql(Response, sql);
    }

    private void saveshopexpense()
    {
        string sbr = "";
        string userid = Request.Params["userid"];
        string shopid = Request.Params["shopid"];
        string paydate = DateTime.Now.AddDays(-1).ToString("yyyy-MM-dd");//Request.Params["paydate"];
        string paymoney = Request.Params["paymoney"];
        string remark = Request.Params["remark"];
        string sql = @"insert into Bas_ShopExpens(shopcode,paydate,paymoney,remark,creator,createdate,checked) 
                        values('"+shopid+"','"+paydate+"','"+paymoney+"','"+remark+"','"+userid+"',getdate(),1)";
        if (mydb.DM_ExecuteNonQuery(sql) != -1)
        {
            sbr = "{\"status\":\"1\",\"msg\":\"操作成功！\"}";
        }
        else
        {
            sbr = "{\"status\":\"0\",\"msg\":\"出现错误！\"}";
        }
        Response.Write(sbr);
        Response.End();
    }

    private void GetVipSaleByShop()
    {
        string shopid = Request.Params["shopid"];
        string shop = " ";
        if (!string.IsNullOrEmpty(shopid))
        {
            shop += "(";
            string[] list = shopid.Split(';');
            foreach (string s in list)
            {
                shop += "''" + s + "'',";
            }
            shop = shop.TrimEnd(',');
            shop += ")";
        }

        string bdate =Request.Params["bdate"];
        if (string.IsNullOrEmpty(bdate))
            bdate = DateTime.Now.ToString("yyyy-MM-dd");
        GetDataToJsonBySql2(Response, "exec KM_GetVipSaleByShop '"+bdate+"','"+shop+"'");
    }

    private void getaccount()
    {
        string shopid = Request.Params["shopid"];
        object result = null;
        DataSet ds = mydb.DM_GetDateSet("select *from Bas_ShopBegin where shopcode ='"+shopid+"'");
        if (ds != null)
        {
            if (ds.Tables[0].Rows.Count > 0)
            {
                result = new
                {
                    accountname = ds.Tables[0].Rows[0]["accountname"],
                    accountcode = ds.Tables[0].Rows[0]["accountcode"],
                    accountbank = ds.Tables[0].Rows[0]["accountbank"],
                };
            }
            else
            {
                result = new { status = 1, msg = "没有数据！" };
            }
        }
        else
        {
            result = new { status = 0, msg = "出现错误" };
        }
        Response.Write(JsonHelper.SerializeObject(result));
        Response.End();
    }

    private void getmypaylog()
    {
        string shopid = Request.Params["shopid"];
        string pageindex = Request.Params["pageindex"];
        string pagecount = Request.Params["pagecount"];
        int showcount = Convert.ToInt32(pageindex) * Convert.ToInt32(pagecount);
        string sql = @"select top " + pagecount + @" *
                        from 
                        (
                            select top 3 row_number() over(order by DzDate desc) as rownumber,right(convert(varchar(20),ReconDate,23),5)as ReconDate,
                            DzDate,curramount2 as paymoney from KM_Shops_StateMent where shopcode='" + shopid 
                            + @"' and isnull(DzDate,'') <>''
                        ) A
                        where rownumber > " + showcount.ToString();

        
        GetDataToJsonBySql(Response, sql);
    }

    private void upshopstate()
    {
        string sbr = "";
        string userid = Request.Params["userid"];
        string bdate = Request.Params["bdate"];
        string shopid = Request.Params["shopid"];
        string sql = @"update KM_Shops_StateMent set DzDate=getdate() where shopcode='"+shopid+"' and ReconDate='"+bdate+"'";
        if (mydb.DM_ExecuteNonQuery(sql) !=-1)
        {
            sbr = "{\"status\":\"1\",\"msg\":\"操作成功！\"}";
        }
        else
        {
            sbr = "{\"status\":\"0\",\"msg\":\"出现错误！\"}";
        }
        Response.Write(sbr);
        Response.End();
    }

    private void getmyshopdzdtl()
    {
        string shopid = Request.Params["shopid"];
        string bdate = Request.Params["bdate"];
        string sql = @"select '" + bdate + @"'as saledate,bankamount,cashamount,otheramount,realpayamount,pay1,pay2,sjpayclamt2,endamount,paycompamt,
                        sjpayclamt,dzstate,paycount from V_CustomerAccountDtl
                        where shopcode='" + shopid + "' and saledate='" + bdate + "'";  
        GetDataToJsonBySql(Response,sql);
    }

    private void getmyshopdzlist()
    {
        string userid = Request.Params["userid"];
        string type = Request.Params["stype"];
        string pageindex = Request.Params["pageindex"];
        string pagecount = Request.Params["pagecount"];
        int showcount = Convert.ToInt32(pageindex) * Convert.ToInt32(pagecount);
        string sbr = "";
        string sql = @"select top " + pagecount + @" *
                        from 
                        (
                            select row_number() over(order by recondate desc) as rownumber,
                            a.shopcode,s.shopname,convert(varchar(20),recondate,23)as recondate,curramount2 from [KM_Shops_StateMent] a
                                            inner join v_shop s on s.Shopcode = a.shopcode                                          
                                            inner join Sys_UserDataRights b on b.SourceID = s.StockID
                                            where cwstate =1 " + (type == "0" ? " and isnull(dzdate,'')='' " : " and isnull(dzdate,'')<>'' ") 
                                            + " and userid=" + userid + @"
                        ) A
                        where rownumber > " + showcount.ToString();

        DataSet ds = mydb.DM_GetDateSet(sql);
        if (ds != null)
        {
            if (ds.Tables[0].Rows.Count == 0)
            {
                sbr = "{\"status\":\"1\",\"msg\":\"没有数据！\"}";
            }
            else
            {
                sbr = DataSetToJson(ds);
            }
        }
        else
        {
            sbr = "{\"status\":\"0\",\"msg\":\"出现错误！\"}";
        }
        Response.Write(sbr);
        Response.End();
    }
    
    private void getmyshopmoney()
    {
        string sbr = "";
        string userid = Request.Params["userid"];
        string bdate = DateTime.Now.ToString("yyyy-MM-dd");
        string sql = @"select s.shopcode,s.shopname,isnull(cast(sum(a.PayAmount) as int),0) as daymoney from v_shop s
                    inner join Sys_UserDataRights b on b.SourceID = s.StockID
                    left join V_PosSale a on s.shopid = a.shopid and cast(convert(varchar(20),a.BillDate,23) as datetime) = '" + bdate + @"'
                    where b.UserID = " + userid+" group by s.shopcode,s.shopname ";

        DataSet ds = mydb.DM_GetDateSet(sql);
        if (ds.Tables[0].Rows.Count > 0)
        {

            sbr = "{\"status\":\"1\","
                + "\"shopcode\":\"" + ds.Tables[0].Rows[0]["shopcode"] + "\","
                + "\"shopname\":\"" + ds.Tables[0].Rows[0]["shopname"] + "\","
                + "\"today\":\"" + DateTime.Now.ToString("yyyy年MM月dd日") + "\","
                + "\"daymoney\":\"" + ds.Tables[0].Rows[0]["daymoney"] + "\""
                +"}";
        }
        else
        {
            sbr = "{\"status\":\"0\",\"msg\":\"出现错误！\"}";
        }
        Response.Write(sbr);
        Response.End();
    }
}